package com.example.yifei.ui.page.main.purchase

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.annotation.ExperimentalCoilApi
import com.example.yifei.ui.page.main.purchase.tab.*
import com.example.yifei.ui.page.widgets.TextTabBar
import com.example.yifei.ui.page.widgets.TitleBar
import com.example.yifei.utils.AppUserUtil
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerState
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * @Author: ZhuDaiHao
 * @Date: 2022/5/12
 * @Describe: 采购列表
 */

@ExperimentalCoilApi
@ExperimentalMaterialApi
@ExperimentalPagerApi
@Composable
fun PurchasedPage(
    navCtrl: NavHostController,
    viewModel: PurchasedViewModel = hiltViewModel()
) {
    Column {
        val pagerState = rememberPagerState(initialPage = AppUserUtil.listIndex)
        val scopeState = rememberCoroutineScope()

        //设置滑动到指定tab页
        scopeState.launch {
            pagerState.scrollToPage(AppUserUtil.listIndex)
        }
        //监听滑动
        LaunchedEffect(pagerState) {
            snapshotFlow { pagerState.currentPage }.collect { index ->
                AppUserUtil.listIndex = index
            }
        }

        TitleBar(title = "采购列表", onClickSearch = {
            //搜索

        }, onClickScreen = {
            //筛选

        })

        TabBar(pagerState, viewModel, scopeState)

        HorizontalPager(
            count = viewModel.tabTitleList.size,
            state = pagerState,
            modifier = Modifier
                .background(Color(0xffF4F6FB))
                .padding(bottom = 56.dp, start = 8.dp, end = 8.dp)
        ) { page ->
            when (page) {
                0 -> PurchaseAllPage(navCtrl)
                1 -> UploadedPage(navCtrl)
                2 -> ConfirmPage(navCtrl)
                3 -> UsedPage(navCtrl)
                4 -> SuccessPage(navCtrl)
                5 -> AbnormalPage(navCtrl)
            }
        }
    }


}


@ExperimentalPagerApi
@Composable
private fun TabBar(
    pagerState: PagerState,
    viewModel: PurchasedViewModel,
    scopeState: CoroutineScope
) {
    Row {
        TextTabBar(
            index = pagerState.currentPage,
            tabTexts = viewModel.tabTitleList,
            onTabSelected = { index ->
                scopeState.launch {
                    pagerState.scrollToPage(index)
                }
            }
        )
    }
}